package com.bj58.mis.apm.server.kylin.common.hllc;

import java.io.Serializable;

public class SingleValueRegister
        implements Register, Serializable
{
    private int singleValuePos;
    private byte value;

    public SingleValueRegister()
    {
        this.singleValuePos = -1;
    }

    public void set(int pos, byte value)
    {
        assert ((this.singleValuePos < 0) || (this.singleValuePos == pos));
        this.singleValuePos = pos;
        this.value = value;
    }

    public byte get(int pos)
    {
        if (pos != this.singleValuePos) {
            return 0;
        }
        return this.value;
    }

    public void merge(Register another)
    {
        throw new IllegalStateException();
    }

    public void clear()
    {
        this.singleValuePos = -1;
    }

    public int getSize()
    {
        if (this.singleValuePos >= 0) {
            return 1;
        }
        return 0;
    }

    public RegisterType getRegisterType()
    {
        return RegisterType.SINGLE_VALUE;
    }

    public int getSingleValuePos()
    {
        return this.singleValuePos;
    }

    public byte getValue()
    {
        return this.value;
    }

    public SparseRegister toSparse()
    {
        SparseRegister sr = new SparseRegister();
        if (this.singleValuePos >= 0) {
            sr.set(this.singleValuePos, this.value);
        }
        return sr;
    }

    public DenseRegister toDense(int p)
    {
        DenseRegister dr = new DenseRegister(p);
        if (this.singleValuePos >= 0) {
            dr.set(this.singleValuePos, this.value);
        }
        return dr;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        SingleValueRegister other = (SingleValueRegister)obj;
        if ((this.singleValuePos != other.singleValuePos) || (this.value != other.value)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }
}